using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Xeml.Document.Contracts;
using Xeml.Document;
using Xeml.Manager;
using Xeml.Sampling.Contracts;


namespace Xid.Controls
{
    public partial class DocumentResourceCtrl : UserControl
    {

        private IDocument xemlDoc;

        public IDocument XemlDoc
        {
            get { return xemlDoc; }
            set
            {
                if (xemlDoc != value)
                {
                    xemlDoc = value;
                    Init();
                }
            }
        }

        private void Init()
        {
            this.flowLayoutPanel1.Controls.Clear();

            if (xemlDoc != null)
            {
                //xemlDoc.DocumentResources.EnvironmentOntologyHandler.ItemAdded += new EventHandler<Mpi.Common.Collections.ItemEventArgs<OntologyHandlerResource>>(EnvironmentOntologyHandler_ItemAdded);
                //xemlDoc.DocumentResources.EnvironmentOntologyHandler.ItemRemoved += new EventHandler<Mpi.Common.Collections.ItemEventArgs<OntologyHandlerResource>>(EnvironmentOntologyHandler_ItemRemoved);
                //xemlDoc.DocumentResources.EnvironmentOntologyHandler.Cleared += new EventHandler(EnvironmentOntologyHandler_Cleared);

                //xemlDoc.DocumentResources.OrganismStructureOntologyHandler.ItemAdded += new EventHandler<Mpi.Common.Collections.ItemEventArgs<OntologyHandlerResource>>(OrganismStructureOntologyHandler_ItemAdded);
                //xemlDoc.DocumentResources.OrganismStructureOntologyHandler.ItemRemoved += new EventHandler<Mpi.Common.Collections.ItemEventArgs<OntologyHandlerResource>>(OrganismStructureOntologyHandler_ItemRemoved);
                //xemlDoc.DocumentResources.OrganismStructureOntologyHandler.Cleared += new EventHandler(OrganismStructureOntologyHandler_Cleared);

                xemlDoc.DocumentResources.DataProvider.ItemAdded += new EventHandler<Mpi.Common.Collections.ItemEventArgs<DataProviderResource>>(SampleProviderRessources_ItemAdded);
                xemlDoc.DocumentResources.DataProvider.ItemRemoved += new EventHandler<Mpi.Common.Collections.ItemEventArgs<DataProviderResource>>(SampleProviderRessources_ItemRemoved);
                xemlDoc.DocumentResources.DataProvider.Cleared += new EventHandler(SampleProviderRessources_Cleared);
            
                //foreach (ParameterValidationResource pvr in xemlDoc.DocumentRessources.ParameterValidationRessources)
                //{
                //    AddValidation(pvr);
                //}

                foreach (OntologyHandlerResource ohr in xemlDoc.DocumentResources.EnvironmentOntologyHandler)
                {
                    AddingOntologyHandler((OntologyHandlerResource)ohr);
                }

                foreach (OntologyHandlerResource ohr in xemlDoc.DocumentResources.GermPlasmOntologyHandler)
                {
                    AddingOntologyHandler(ohr);
                }

                foreach (OntologyHandlerResource ohr in xemlDoc.DocumentResources.OrganismStructureOntologyHandler)
                {
                    AddingOntologyHandler(ohr);
                }

                foreach (OntologyHandlerResource ohr in xemlDoc.DocumentResources.DevelopmentalOntologyHandler)
                {
                    AddingOntologyHandler(ohr);
                }

                foreach (OntologyHandlerResource ohr in xemlDoc.DocumentResources.PositioningOntologyHandler)
                {
                    AddingOntologyHandler(ohr);
                }

                foreach (DataProviderResource spr in xemlDoc.DocumentResources.DataProvider)
                {
                    AddProvider(spr);
                }
            }
        }

        void EnvironmentOntologyHandler_Cleared(object sender, EventArgs e)
        {
            List<Control> toRem = new List<Control>();
            foreach (Control c in this.flowLayoutPanel1.Controls)
            {
                if (c is ValidationResCtrl)
                {
                    if ((c as ValidationResCtrl).OntologyResource.ComponentState == ComponentLoadState.Loaded &&
                        (c as ValidationResCtrl).OntologyResource.Handler is IDynamicOntologyHandler)
                    {
                        toRem.Add(c);
                    }
                }
            }
            foreach (Control c in toRem)
            {
                this.flowLayoutPanel1.Controls.Remove(c);
            }
        }

        void EnvironmentOntologyHandler_ItemRemoved(object sender, Mpi.Common.Collections.ItemEventArgs<OntologyHandlerResource> e)
        {
            Control  toRem = null;

            foreach (Control c in this.flowLayoutPanel1.Controls)
            {
                if (c is ValidationResCtrl)
                {
                    if ((c as ValidationResCtrl).OntologyResource == e.Item)
                    {
                        toRem = c;
                        break;
                    }
                }
            }
            if(toRem != null)
                this.flowLayoutPanel1.Controls.Remove(toRem);
        }

        void EnvironmentOntologyHandler_ItemAdded(object sender, Mpi.Common.Collections.ItemEventArgs<OntologyHandlerResource> e)
        {
            AddingOntologyHandler(e.Item);
        }

        void OrganismStructureOntologyHandler_Cleared(object sender, EventArgs e)
        {
            List<Control> toRem = new List<Control>();
            foreach (Control c in this.flowLayoutPanel1.Controls)
            {
                if (c is ValidationResCtrl)
                    toRem.Add(c);
            }
            foreach (Control c in toRem)
            {
                this.flowLayoutPanel1.Controls.Remove(c);
            }
        }

        void OrganismStructureOntologyHandler_ItemRemoved(object sender, Mpi.Common.Collections.ItemEventArgs<OntologyHandlerResource> e)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        void OrganismStructureOntologyHandler_ItemAdded(object sender, Mpi.Common.Collections.ItemEventArgs<OntologyHandlerResource> e)
        {
            AddingOntologyHandler(e.Item);
        }

        private void AddingOntologyHandler(OntologyHandlerResource ohr)
        {
            ValidationResCtrl dpc = new ValidationResCtrl();
            
            dpc.ShowDetails = false;
            dpc.OntologyResource = ohr;
            this.flowLayoutPanel1.Controls.Add(dpc);
        }

        void SampleProviderRessources_Cleared(object sender, EventArgs e)
        {
            List<Control> toRem = new List<Control>();
            foreach (Control c in this.flowLayoutPanel1.Controls)
            {
                if (c is SampleProviderCtrl)
                    toRem.Add(c);
            }
            foreach (Control c in toRem)
            {
                this.flowLayoutPanel1.Controls.Remove(c);
            }
        }

        void ParameterValidationRessources_Cleared(object sender, EventArgs e)
        {
            List<Control> toRem = new List<Control>();
            foreach (Control c in this.flowLayoutPanel1.Controls)
            {
                if (c is ValidationResCtrl)
                    toRem.Add(c);
            }
            foreach (Control c  in toRem)
            {
                this.flowLayoutPanel1.Controls.Remove(c);
            }
        }

        //void ParameterValidationRessources_ItemRemoved(object sender, Mpi.Common.Collections.ItemEventArgs<OntologyHandlerResource> e)
        //{
        //    throw new Exception("The method or operation is not implemented.");
        //}

        void SampleProviderRessources_ItemRemoved(object sender, Mpi.Common.Collections.ItemEventArgs<DataProviderResource> e)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        //void ParameterValidationRessources_ItemAdded(object sender, Mpi.Common.Collections.ItemEventArgs<ParameterValidationResource> e)
        //{
        //    AddValidation(e.Item);
        //}

        //private void AddValidation(ParameterValidationResource pvr)
        //{

        //    ValidationResCtrl dpc = new ValidationResCtrl();
        //    dpc.ShowDetails = false;
        //    dpc.ParameterResource = pvr;
        //    this.flowLayoutPanel1.Controls.Add(dpc);

        //    //bool compLoded;
            
        //    //IParameterValidator pr = null;
        //    //try
        //    //{
        //    //    pr = ValidatorManager.Instance.CreateParameterValidator(pvr.ComponentId, pvr.NameSpace, pvr.Location);
        //    //    compLoded = true;
        //    //}
        //    //catch (XemlComponentLoadFailedException)
        //    //{
        //    //    compLoded = false;
        //    //}
        //    //if (compLoded)
        //    //{
        //    //    ValidationResCtrl dpc = new ValidationResCtrl();
        //    //    dpc.ShowDetails = false;
               
        //    //    Binding connstrBind = new Binding("ParamSpecLocation", pvr, "Location");
        //    //    connstrBind.ControlUpdateMode = ControlUpdateMode.OnPropertyChanged;
        //    //    connstrBind.DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
        //    //    dpc.DataBindings.Add(connstrBind);

        //    //    Binding fnBind = new Binding("NameSpace", pvr, "NameSpace");
        //    //    connstrBind.ControlUpdateMode = ControlUpdateMode.OnPropertyChanged;
        //    //    connstrBind.DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
        //    //    dpc.DataBindings.Add(fnBind);

        //    //    //dpc.Provider = prov;
        //    //    dpc.CompId = pr.ComponentId.ToString("B");

        //    //    dpc.Publisher = pr.Publisher;
        //    //    dpc.Version = pr.Version;

        //    //    this.flowLayoutPanel1.Controls.Add(dpc);

        //    //}
        //}

        private void AddProvider(DataProviderResource spr)
        {

            //bool compLoded;
            //ISampleProvider prov = null;
            //try
            //{
            //    prov = SpManager.Instance.CreateProvider(spr.ComponentId, spr.FriendlyName, spr.ConnectionString);
            //    compLoded = true;
            //}
            //catch (XemlComponentLoadFailedException ex)
            //{
            //    compLoded = false;
            //}


            if (spr.ComponentState == ComponentLoadState.Loaded)
            {
                ProviderCard pc = new ProviderCard(xemlDoc);
                pc.DataProvider = spr;
                //Binding connstrBind = new Binding("ConnectionString", spr, "ConnectionString");
                //connstrBind.ControlUpdateMode = ControlUpdateMode.OnPropertyChanged;
                //connstrBind.DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
                //dpc.DataBindings.Add(connstrBind);

                //Binding fnBind = new Binding("FriendlyName", spr, "FriendlyName");
                //fnBind.ControlUpdateMode = ControlUpdateMode.OnPropertyChanged;
                //fnBind.DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
                //pc.DataBindings.Add(fnBind);

                
                //dpc.CompId = spr.Provider.ComponentId.ToString("B");

                this.flowLayoutPanel1.Controls.Add(pc);
            }
        }

        void SampleProviderRessources_ItemAdded(object sender, Mpi.Common.Collections.ItemEventArgs<DataProviderResource> e)
        {
            AddProvider(e.Item);
        }

        public DocumentResourceCtrl()
        {
            InitializeComponent();
        }

        private void sampleProviderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //using (DataProviderWizard dpwiz = new DataProviderWizard())
            //{
            //    dpwiz.ShowDialog(this.ParentForm);
            //    if (dpwiz.ProviderResource != null)
            //        xemlDoc.DocumentResources.DataProvider.Add(dpwiz.ProviderResource);
            //}

            using (DataConnectionWizard dcwiz = new DataConnectionWizard())
            {
                dcwiz.ShowDialog(this.ParentForm);
                if (dcwiz.ProviderResource != null)
                {
                    xemlDoc.DocumentResources.DataProvider.Add(dcwiz.ProviderResource);
                }
            }
            
            //SampleProviderWizard dr = new SampleProviderWizard();
            //dr.WizardFinished += new EventHandler<SampleProviderWizardEventArgs>(dr_WizardFinished);
            //this.flowLayoutPanel1.Controls.Add(dr);
        }

        void dr_WizardFinished(object sender, SampleProviderWizardEventArgs e)
        {
            if (e.Provider != null)
            {
                xemlDoc.DocumentResources.DataProvider.Add(e.Provider);
            }
            this.flowLayoutPanel1.Controls.Remove(sender as SampleProviderWizard);
            (sender as SampleProviderWizard).Dispose();
        }

        private void parameterResourceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddOntologyWizard aow = new AddOntologyWizard(this.xemlDoc);
            DialogResult dr = aow.ShowDialog(this);
            if (dr == DialogResult.OK)
            {
                AddingOntologyHandler(aow.AddedHandlerResource);
            }

            //arw.WizardFinished += new EventHandler<ValidationResWizardEventArgs>(arw_WizardFinished);
            //this.flowLayoutPanel1.Controls.Add(arw);
        }

        private void toolStripDropDownButton1_Click(object sender, EventArgs e)
        {
            OntologyHandlerResource toRemove = null;
            Control toremCtrl = null;
            foreach (Control c in this.flowLayoutPanel1.Controls)
            {
                if (c is ValidationResCtrl)
                {
                    if ((c as ValidationResCtrl).Selected)
                    {
                        toRemove = (c as ValidationResCtrl).OntologyResource;
                        toremCtrl = c;
                    }
                }

                if (c is SampleProviderCtrl)
                {
                    //if ((c as SampleProviderCtrl).Selected)
                    //{
                    //    toRemove = (c as SampleProviderCtrl).SampleProviderRes;
                    //}
                }
            }
            if (toremCtrl != null)
            {
                flowLayoutPanel1.Controls.Remove(toremCtrl);
            }
            if (toRemove != null)
            {
                this.xemlDoc.DocumentResources.Remove(toRemove);
            }
        }
    }
}
